package com.geeyuu.android.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.geeyuu.android.GeePrefrence;
import com.geeyuu.android.databases.DataBaseHelper;
import com.geeyuu.android.utils.CommonUtil;
import com.geeyuu.android.utils.DBUtil;
import com.geeyuu.android.utils.LogUtil;

public class ChatInfo {

    //消息类型
    public static final int MSG_TYPE_TEXT = 1;
    public static final int MSG_TYPE_VOICE = 2;
    public static final int MSG_TYPE_IMAGE = 3;
    public static final int MSG_TYPE_LOCATION = 4;
    public static final int MSG_TYPE_EXCHANGE_PHOTO = 5;
    public static final int MSG_TYPE_EXCHANGE_PHOTO_REPLY = 6;
    
    //消息发送状态
    public static final int SEND_STATE_SENDING = 1;
    public static final int SEND_STATE_ARRIVED = 2;
    public static final int SEND_STATE_FAIL = 3;
    
    //语音播放状态
    public static final int VOICE_STATE_PLAYING = 1; //正在播放的状态
    public static final int VOICE_STATE_PLAYED = 2; //已经播放过的状态
    public static final int VOICE_STATE_NOT_PLAYED = 3; //还没播放过的状态
    
    /**
     * 文档没写清，暂时用0
     */
    public static final int EXCHANGED_DISAGREED = 0;
    public static final int EXCHANGED_AGREED = 1;
    
    public static final String CHAT_ID = "_id";
    public static final String FRIEND_UID = "friend_uid";
    public static final String SEND_UID = "send_uid";
    public static final String RECEIVE_UID = "receive_uid";
    public static final String CHAT_CONTENT = "chat_content";
    public static final String SEND_DATE = "send_date";
    public static final String MSG_TYPE = "msg_type";
    public static final String CHAT_SEND_STATE = "chat_send_state";
    public static final String VOICE_PLAY_STATE = "voice_play_state";
    public static final String VOICE_FILE_ID = "voice_file_id";
    public static final String LON_AND_LAT = "lon_and_lat";
    public static final String CHAT_MARK_ID = "chat_mark_id";
    
    public static final String TABLE_NAME = "ChatInfo";
    
    private static final String TAG = "ChatInfo";
    
    public void setChatInfo(Cursor c){
        chatId = 0;
        friendUid = 0l;
        sendUid = 0l;
        recUid = 0l;
        chatContent = "";
        sendDate = 0l;
        msgType = 0;
        sendState = 0;
        voicePlayState = 0;
        voiceFileId = "";
        lonAndLat = "";
        chatMarkId = 0l;
        if(null != c){
            chatId = DBUtil.getInt(c, CHAT_ID);
            friendUid = DBUtil.getInt(c, FRIEND_UID);
            sendUid = DBUtil.getInt(c, SEND_UID);
            chatContent = DBUtil.getString(c, CHAT_CONTENT);
            sendDate = DBUtil.getLong(c, SEND_DATE);
            msgType = DBUtil.getInt(c, MSG_TYPE);
            sendState = DBUtil.getInt(c, CHAT_SEND_STATE);
            voicePlayState = DBUtil.getInt(c, VOICE_PLAY_STATE);
            voiceFileId = DBUtil.getString(c, VOICE_FILE_ID);
            lonAndLat = DBUtil.getString(c, LON_AND_LAT);
            chatMarkId = DBUtil.getLong(c, CHAT_MARK_ID);
        }else{
            LogUtil.e(TAG, "设置聊天信息时，cursor is null");
        }
    }
    
    public static void updateChatInfo(Context context, ContentValues cv){
        Integer chatId = cv.getAsInteger("chatId");
        if(null != chatId){
            if(!DataBaseHelper.updateChatInfo(context, cv, chatId)){
                LogUtil.e(TAG, "更新聊天记录失败");
            }
        }else{
            LogUtil.e(TAG, "更新聊天记录出错，没有chatId");
        }
    }
    
    public static long saveChatInfo(Context context, ContentValues cv){
        long id = DataBaseHelper.saveChatInfo(context, cv);
        if(id == -1){
            LogUtil.e(TAG, "插入聊天记录失败");
        }
        return id;
    }
    
    public long chatId;
    public long friendUid;
    public long sendUid;
    public long recUid;
    
    public String chatContent;
    public long sendDate;
    public int msgType;
    public int sendState;
    public int voicePlayState = VOICE_STATE_NOT_PLAYED;
    
    public String voiceFileId;
    public String lonAndLat;
    
    public long chatMarkId;
    
    public long saveChatInfo(Context context){
        final long myUid = GeePrefrence.getMyUid(context);
        ContentValues cv = new ContentValues();
        cv.put(ChatInfo.FRIEND_UID, friendUid);
        cv.put(ChatInfo.MSG_TYPE, msgType);
        cv.put(ChatInfo.SEND_UID, sendUid);
        cv.put(ChatInfo.RECEIVE_UID, recUid);
        cv.put(ChatInfo.CHAT_CONTENT, chatContent);
        if(sendUid == myUid){
            final long nowTime = System.currentTimeMillis();
            sendDate = nowTime;
            cv.put(ChatInfo.SEND_DATE, nowTime);
            cv.put(ChatInfo.CHAT_SEND_STATE, SEND_STATE_SENDING);
        }else{
            cv.put(ChatInfo.SEND_DATE, sendDate);
        }
        if(msgType == ChatInfo.MSG_TYPE_VOICE && sendUid != myUid){
            cv.put(ChatInfo.VOICE_PLAY_STATE, voicePlayState);
        }
        if(msgType == ChatInfo.MSG_TYPE_LOCATION){
            if(sendUid == myUid){
                final String loc = GeePrefrence.getMyLocation(context);
                lonAndLat = loc;
                cv.put(ChatInfo.LON_AND_LAT, loc);
            }else{
                cv.put(ChatInfo.LON_AND_LAT, lonAndLat);
            }
        }
        if(msgType == ChatInfo.MSG_TYPE_VOICE){
            if(sendUid == myUid){
                final String str = CommonUtil.getRandomStr();
                voiceFileId = str;
                cv.put(VOICE_FILE_ID, str);
            }else{
                cv.put(VOICE_FILE_ID, voiceFileId);
            }
        }
        chatId = ChatInfo.saveChatInfo(context, cv);
        return chatId;
    }
    
}
